-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResource.cs
144 lines (138 loc) · 7.5 KB
/
Resource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
namespace My
{
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Reflection;
/// <summary>
/// 程序资源文件相关函数
/// </summary>
public sealed partial class Resource
{
/// <summary>
/// 读取程序嵌入的资源文件(注意必须在解决方案资源管理器中,将资源文件的"属性"-"生成操作"设置为"嵌入的资源")
/// </summary>
/// <param name="ResourceName">资源文件名称(注意这个参数的值为资源文件在工程内的相对路径,例如A.zip文件在Resources文件夹内,则此处的参数应为"Resources.A.zip")</param>
/// <returns>结果Byte数组(失败返回空Byte数组)</returns>
public static byte[] ReadByte(string ResourceName)
{
try
{
Assembly executingAssembly = Assembly.GetExecutingAssembly();
Stream manifestResourceStream = executingAssembly.GetManifestResourceStream(executingAssembly.GetName().Name + "." + ResourceName);
byte[] buffer2 = new byte[manifestResourceStream.Length];
manifestResourceStream.Read(buffer2, 0, buffer2.Length);
manifestResourceStream.Dispose();
return buffer2;
}
catch (Exception ex)
{
return new byte[0];
}
}
/// <summary>
/// 读取程序嵌入的图片类型的资源文件(注意必须在解决方案资源管理器中,将资源文件的"属性"-"生成操作"设置为"嵌入的资源")
/// </summary>
/// <param name="ResourceName">资源文件名称(注意这个参数的值为资源文件在工程内的相对路径,例如A.jpg文件在Resources文件夹内,则此处的参数应为"Resources.A.jpg")</param>
/// <returns>结果图片(失败返回1*1个像素,#00000000透明色的图片)</returns>
public static Bitmap ReadPicture(string ResourceName)
{
try
{
Assembly executingAssembly = Assembly.GetExecutingAssembly();
return new Bitmap(executingAssembly.GetManifestResourceStream(executingAssembly.GetName().Name + "." + ResourceName));
}
catch (Exception ex)
{
return new Bitmap(1, 1);
}
}
/// <summary>
/// 读取程序嵌入的文本类型的资源文件(注意必须在解决方案资源管理器中,将资源文件的"属性"-"生成操作"设置为"嵌入的资源")
/// </summary>
/// <param name="ResourceName">资源文件名称(注意这个参数的值为资源文件在工程内的相对路径,例如A.txt文件在Resources文件夹内,则此处的参数应为"Resources.A.txt")</param>
/// <returns>结果字符串(失败返回空字符串)</returns>
public static string ReadString(string ResourceName)
{
try
{
Assembly executingAssembly = Assembly.GetExecutingAssembly();
Stream manifestResourceStream = executingAssembly.GetManifestResourceStream(executingAssembly.GetName().Name + "." + ResourceName);
byte[] temp = new byte[manifestResourceStream.Length];
manifestResourceStream.Read(temp, 0, temp.Length);
manifestResourceStream.Dispose();
return Encoding.UTF8.GetString(temp);
}
catch (Exception ex)
{
return "";
}
}
/// <summary>
/// 读取程序嵌入的文本类型的资源文件(注意必须在解决方案资源管理器中,将资源文件的"属性"-"生成操作"设置为"嵌入的资源")
/// </summary>
/// <param name="ResourceName">资源文件名称(注意这个参数的值为资源文件在工程内的相对路径,例如A.txt文件在Resources文件夹内,则此处的参数应为"Resources.A.txt")</param>
/// <param name="Encoding">使用特定的字符编码(默认UTF-8)</param>
/// <returns>结果字符串(失败返回空字符串)</returns>
public static string ReadString(string ResourceName, Encoding Encoding)
{
try
{
Assembly executingAssembly = Assembly.GetExecutingAssembly();
Stream manifestResourceStream = executingAssembly.GetManifestResourceStream(executingAssembly.GetName().Name + "." + ResourceName);
byte[] temp = new byte[manifestResourceStream.Length];
manifestResourceStream.Read(temp, 0, temp.Length);
manifestResourceStream.Dispose();
return Encoding.UTF8.GetString(temp);
}
catch (Exception ex)
{
return "";
}
}
/// <summary>
/// 读取程序嵌入的字符串数组类型的资源文件(注意必须在解决方案资源管理器中,将资源文件的"属性"-"生成操作"设置为"嵌入的资源")
/// </summary>
/// <param name="ResourceName">资源文件名称(注意这个参数的值为资源文件在工程内的相对路径,例如A.txt文件在Resources文件夹内,则此处的参数应为"Resources.A.txt")</param>
/// <returns>结果字符串数组(失败返回空String数组)</returns>
public static string[] ReadStringArray(string ResourceName)
{
try
{
Assembly executingAssembly = Assembly.GetExecutingAssembly();
Stream manifestResourceStream = executingAssembly.GetManifestResourceStream(executingAssembly.GetName().Name + "." + ResourceName);
byte[] temp = new byte[manifestResourceStream.Length];
manifestResourceStream.Read(temp, 0, temp.Length);
manifestResourceStream.Dispose();
return Encoding.UTF8.GetString(temp).Replace("\r\n", "\n").Split(new char[] { '\n' });
}
catch (Exception ex)
{
return new string[0];
}
}
/// <summary>
/// 读取程序嵌入的字符串数组类型的资源文件(注意必须在解决方案资源管理器中,将资源文件的"属性"-"生成操作"设置为"嵌入的资源")
/// </summary>
/// <param name="ResourceName">资源文件名称(注意这个参数的值为资源文件在工程内的相对路径,例如A.txt文件在Resources文件夹内,则此处的参数应为"Resources.A.txt")</param>
/// <param name="Encoding">使用特定的字符编码(默认UTF-8)</param>
/// <returns>结果字符串数组(失败返回空String数组)</returns>
public static string[] ReadStringArray(string ResourceName, Encoding Encoding)
{
try
{
Assembly executingAssembly = Assembly.GetExecutingAssembly();
Stream manifestResourceStream = executingAssembly.GetManifestResourceStream(executingAssembly.GetName().Name + "." + ResourceName);
byte[] temp = new byte[manifestResourceStream.Length];
manifestResourceStream.Read(temp, 0, temp.Length);
manifestResourceStream.Dispose();
return Encoding.GetString(temp).Replace("\r\n", "\n").Split(new char[] { '\n' });
}
catch (Exception ex)
{
return new string[0];
}
}
}
}